/** @jsxImportSource @emotion/react */
import * as React from "react";
import {
    CCell, Cell, Input, InputDatalist, InputIdProvider, InputLine, LineColumn, SuffixInput, Table, TableBody, TableHead, TableRow, Text, TextArea,
} from "customize-easy-ui-component";
import {InspectRecordLayout, InternalItemProps, RepLink, SelectHookfork, useItemInputControl,} from "../../common/base";
import {useMeasureInpFilter} from "../../common/hooks";
import {floatInterception, objNestSet} from "../../../common/tool";
import {useOmnipotentPref, useOmnipotentPrefTitle} from "../../hook/useOmnipotentPref";

export const 检验结果选=['√','／','×','△'];
const tailZj=<Text css={{"@media print": {fontSize: '0.75rem'} }}>
    注：1、对于产品标准和设计文件同时对速度允许偏差都有规定的，以较严规定作为检验结果判定依据。对于产品标准和
    设计文件对速度允许偏差都没有规定的，相应的速度仅测量，不作检验结果判定。<br/>
    2、对于多起升机构或多小车运行机构的起重机，仅记录其中2个主起升机构和2个主小车运行机构的速度。对于其余起
    升机构和小车运行机构的速度测量值，记录在备注栏。<br/>
    3、以设计文件作为检验结果判定依据时，设计要求值填在备注栏内。<br/>
    4、未检查或无需检验的，仅填检验结果栏。
</Text>;
export interface Props extends InternalItemProps {
    iAname?: string[];
}
export const items机构运速 = [
    ['构1起', [], '主起升机构1起升速度', ],
    ['构1下', [], '主起升机构1下降速度', ],
    ['构2起', [], '主起升机构2起升速度', ],
    ['构2下', [], '主起升机构2下降速度', ],
    ['大车横速', [], '大车横移运行速度', ],
    ['车1纵', [], '小车1纵移运行速度', ],
    ['车2纵', [], '小车2纵移运行速度', ],
];

export const itemA机构运速: string[] = ['运速备注','吊起升速','吊起升速r','吊下降速','纵移运速','台速误差','台速误差r'];
items机构运速.forEach(([name, title, initIsp], i: number) => {
    itemA机构运速.push(name as string);
});

export const MoveSpeed =
    React.forwardRef((
        {children, show, alone = true, refWidth, label, iAname,}: Props, ref
) => {
    const {inp, setInp} = useItemInputControl({ref});
    const itemA = React.useMemo(() => {
        return [ ...iAname ?? [], ...itemA机构运速];
    }, [iAname]);
    const [getInpFilter] = useMeasureInpFilter(null, itemA,);
    const titNode=useOmnipotentPrefTitle({config: items机构运速, });
    return (
        <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show}
                             alone={alone} label={label!}>
            <Text variant="h5">{label}：</Text>
            {items机构运速.map(([name, titlps, item, unit]: any, i: number) => {
                return (<React.Fragment key={i}>
                    <div css={{display: 'flex', flexWrap: 'wrap', alignItems: 'center'}}>
                        {titNode[i]}&nbsp;
                        &nbsp;距离（m）
                        <Input value={inp?.[name]?.d || ''} style={{display: 'inline-flex', width: '4rem'}}
                               onChange={e => objNestSet(name, 'd', e.currentTarget.value, inp, setInp)}/>m
                        &nbsp;时间（min）
                        <Input value={inp?.[name]?.t || ''} style={{display: 'inline-flex', width: '4rem'}}
                               onChange={e => objNestSet(name, 't', e.currentTarget.value, inp, setInp)}/>min
                        &nbsp;速度 =&nbsp;
                        <Text css={{fontSize: '1.1rem'}}>
                            {floatInterception(inp?.[name]?.d / inp?.[name]?.t, 2) || ''} m/min
                        </Text>
                        &nbsp;检验结果
                        <InputIdProvider>
                            <InputDatalist value={(inp?.[name]?.r) || ''} datalist={检验结果选}
                                           style={{display: 'inline-flex', width: '10rem'}}
                                           onListChange={v => objNestSet(name, 'r', v || undefined, inp, setInp)}/>
                        </InputIdProvider>
                    </div>
                </React.Fragment>);
            })}
            <hr/>
            下半部分：
            <Text css={{textIndent: '2rem', display: 'flex', fontSize: '1.2rem'}}>
                速度相对误差-项目 &gt;
            </Text>
            项目： 两台吊梁小车速度相对误差：要求值≤2%；
            <LineColumn>
                <InputLine  label='起升速度-计算值：' >
                    <SuffixInput  value={inp?.吊起升速 ||''} onSave={txt=> setInp({...inp,吊起升速: txt || undefined })}>mm</SuffixInput>
                </InputLine>
                <InputLine  label='下降速度-计算值：' >
                    <SuffixInput  value={inp?.吊下降速 ||''} onSave={txt=> setInp({...inp,吊下降速: txt || undefined })}>mm</SuffixInput>
                </InputLine>
                <InputLine  label='纵移运行速度-计算值：' >
                    <SuffixInput  value={inp?.纵移运速 ||''} onSave={txt=> setInp({...inp,纵移运速: txt || undefined })}>mm</SuffixInput>
                </InputLine>
                <InputLine label='两台吊梁小车速度相对误差-检验结果'>
                    <SelectHookfork value={ inp?.吊起升速r ||''}
                                    onChange={e => setInp({ ...inp, 吊起升速r: e.currentTarget.value||undefined}) }/>
                </InputLine>
            </LineColumn>
            项目： 前吊梁小车运行速度与运梁台车走行速度相对误差：要求值≤2%；
            <LineColumn>
                <InputLine  label='前吊梁小车-计算值：' >
                    <SuffixInput  value={inp?.台速误差 ||''} onSave={txt=> setInp({...inp,台速误差: txt || undefined })}>mm</SuffixInput>
                </InputLine>
                <InputLine label='前吊梁小车-检验结果'>
                    <SelectHookfork value={ inp?.台速误差r ||''}
                                    onChange={e => setInp({ ...inp, 台速误差r: e.currentTarget.value||undefined}) }/>
                </InputLine>
            </LineColumn>
            备注：
            <TextArea  value={inp?.运速备注 ||''} rows={4}
                       onChange={e => setInp({ ...inp, 运速备注: e.currentTarget.value||undefined}) } />
            { children ? children : tailZj }
        </InspectRecordLayout>
    );
});

const tailRender = (orc: any, name: string, lix: number) => <>
    <CCell>{orc?.[name]?.d ?? '／'}</CCell>
    <CCell>{orc?.[name]?.t ?? '／'}</CCell>
    <CCell>{floatInterception(orc?.[name]?.d/orc?.[name]?.t, 2) || ''}</CCell>
    <CCell>{orc?.[name]?.r ?? '／'}</CCell>
</>;
export const MoveSpeedVw = ({orc, rep, label, children, crit}:
                                      { orc: any, rep: any, label: string, children?: any, crit?: boolean }
) => {
    const tag = 'MoveSpeed';
    const [renderSpeedTest] = useOmnipotentPref({
        orc, config: items机构运速, tailRender, pcols: 1, noNo: true, unitCel: false
    });
    return <>
        <Text variant="h4" css={{
            marginTop: '1rem', "@media print": {paddingBottom: '4rem', pageBreakInside: 'avoid'}
        }}>{label}</Text>
        <Table id={tag} fixed={ ["%", "14%", "13%", "13%", "15%", "11%"] } tight miniw={800}
                css={{borderCollapse: 'collapse', "@media print": {marginTop: '-4rem'}}}>
            <TableHead>
                <TableRow>
                    <CCell colSpan={2}>项目</CCell><CCell>距离（m）</CCell><CCell>时间（min）</CCell><CCell>速度（m/min)</CCell><CCell>检验结果</CCell>
                </TableRow>
            </TableHead>
            <TableBody>
            <RepLink ori rep={rep} tag={'MoveSpeed'}>
                {renderSpeedTest}
            </RepLink>
            </TableBody>
        </Table>
        <Text variant="h4" css={{marginBottom: 0,"@media print": {paddingBottom: '4rem',pageBreakInside: 'avoid'}
        }}></Text>
        <Table id={tag} fixed={["13%", "35%", "%", "14%","11%"]} tight miniw={800}
               css={{borderCollapse: 'collapse', "@media print": {marginTop: '-4rem'}}}>
            <TableHead>
                <TableRow>
                    <CCell colSpan={2}>项目</CCell><CCell>计算值</CCell><CCell>要求值</CCell><CCell>检验结果</CCell>
                </TableRow>
            </TableHead>
            <TableBody>
                <RepLink ori rep={rep} tag={'MoveSpeed'}>
                    <TableRow>
                        <CCell rowSpan={3}>两台吊梁小车速度相对误差</CCell><CCell>起升速度</CCell><CCell>{orc?.吊起升速}</CCell>
                        <CCell rowSpan={3}>≤2%</CCell><CCell rowSpan={3}>{orc?.吊起升速r}</CCell>
                    </TableRow>
                    <TableRow><CCell>下降速度</CCell><CCell>{orc?.吊下降速}</CCell></TableRow>
                    <TableRow><CCell>纵移运行速度</CCell><CCell>{orc?.纵移运速}</CCell></TableRow>
                    <TableRow>
                        <CCell colSpan={2}>前吊梁小车运行速度与运梁台车走行速度相对误差</CCell><CCell>{orc?.台速误差}</CCell>
                        <CCell>≤2%</CCell><CCell>{orc?.台速误差r}</CCell>
                    </TableRow>
                    <TableRow>
                        <CCell>备注</CCell>
                        <Cell split={true} colSpan={4}>
                            <div css={{whiteSpace: 'pre-wrap'}}>{orc?.运速备注 || '／'}</div>
                        </Cell>
                    </TableRow>
                </RepLink>
            </TableBody>
        </Table>
        { children ? children : tailZj }
    </>;
};
